home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / SAT / Misc / transSkel.p < prev   
Encoding:
Text File  |  1994-07-26  |  48.8 KB  |  1,636 lines  |  [TEXT/PJMM]

  1. {    TransSkel version 2.00 - Transportable application skeleton}
  2.  
  3. {    TransSkel is public domain and was originally written in LightSpeed C by:}
  4.  
  5. {            Paul DuBois}
  6. {            Wisconsin Regional Primate Research Center}
  7. {            1220 Capital Court}
  8. {            Madison WI  53706  USA}
  9.  
  10. {    UUCP:    [allegra,ihnp4,seismo]!uwvax!rhesus!dubois }
  11. {    ARPA:    dubois@rhesus.primate.wisc.edu}
  12. {    The Pascal Version of TransSkel is public domain and was ported and changed by        }
  13.  
  14. {            Owen Hartnett            }
  15. {            Ωhm Software            }
  16. {            163 Richard Drive        }
  17. {            Tiverton, RI 02878        }
  18.  
  19. {    CSNET:    omh@cs.brown.edu.CSNET                                             }
  20. {    ARPA:        omh%cs.brown.edu                                                    }
  21. {    UUCP:        [ihnp4,allegra]!brunix !omh                                            }
  22.  
  23. {    This version of TransSkel written for Lightspeed Pascal.  Lightspeed Pascal is a}
  24. {    trademark of:}
  25. {            THINK Technologies, Inc}
  26. {            420 Bedford Street  Suite 350}
  27. {            Lexington, MA  02173  USA}
  28.  
  29. {  History}
  30. {  06/13/86    Beta version. (pd) }
  31. {  08/27/86    Version number changed to 1.01.(pd)}
  32. {              v1.0 DoGrow bug fixed - the port at the point of the}
  33. {              InvalRect could have been anything; the fix is to set}
  34. {              the port to the grown window first.  This also explains}
  35. {              why the kludge to DoActivate in v1.0 worked.(pd)}
  36. {  10/02/86    Version number changed to 1.02, as a result of adding}
  37. {            modifications by David W. Berry (well!dwb@lll-lcc.arpa)}
  38. {            for supporting window zooming.  Also used his modifications}
  39. {            for supporting modeless dialogs (though not in the same}
  40. {            form).  Dialogs can be #define'd on or off.(pd)}
  41.  
  42. {12/ 28 / 86 Version number changed to 1.03 . Modified to work under LightspeedC v . 2.01 }
  43. {            - took out definitions for window zooming stuff , as it is now supported by the compiler}
  44. {             directly . Also declared DoZoom static , fixing an oversight . ( pd )}
  45. { 01 / 18 / 86 Put a SetPort into DoZoom - ZoomWindow requires port to be}
  46. {            set to window being zoomed . ( pd )}
  47. { 02 / 05 / 86 Version number changed to 1.04 . Big change : port setting behavior made explicit}
  48. {             - the only persistant switch occurs when a window comes active . This changes }
  49. {            underlying programming model ( see manual for detailed discussion ) . Thanks to}
  50. {             Duane Williams for pointing out that this should be done . Typedef 'd }
  51. {            integer/long variables to Integer, Longint to facilitate coversion to other C }
  52. {            compilers . More complete type-casting done . LightspeedC does a lot of it }
  53. {            automatically , other compilers may not . ( pd - this version never released ) }
  54. {03 / 02 / 87 Fixed bug whereby clicks in drag region of non - active windows may not bring }
  55. {            window to front . Seems to be due to DragWindow calling StillDown to see    if mouse is still }
  56. {            down .    If the machine was busy    otherwise when click occurred and }
  57. {            mouse already up when DragWindow is called , the click ends up being ignored . }
  58. {            Thanks to Roger Humphrey for finding this one . }
  59.  
  60. {* * * Changes implemented first by omh to Pascal Version}
  61.  
  62. { 12/24/86  Finished first Pascal version.   Dialogs cannot be defined off.     (omh)}
  63.  
  64. {4 / 18 / 87 Changed Desk Accessory code so it 's more tolerant of memory}
  65. {            conditions for desk accessories . ( omh ) }
  66. {7 / 12 / 87 Added "cache " code to GetWDHandler . Now TransSkel figures }
  67. {            that an event is most likely to occur for the same window as the previous }
  68. {            event . Thus the WindowPtr and WDHandle for events are cached and examined }
  69. {            to avoid searching through the handler list . ( omh ) }
  70. {7 / 12 / 87 Excised the notorious "SetPort "excess . As pointed out by Duane Williams ,}
  71. {             SetPort traps abounded unnecessarily in version 1.02 . These have been eliminated }
  72. {            now with two exceptions . First , the port is set when a window handler }
  73. {            is installed . The justification for this is that when a handler is installed , it }
  74. {            is likely that further processing will be done on it immediately . The application gets }
  75. {            control immediately after the handler is installed anyway , so this behavior can be manually }
  76. {            overridden where necessary . Second , when a window is activated , the port is}
  77. {            set to it . This follows the model of keeping the port in sync with the }
  78. {            active window . ( omh ) }
  79. {7 / 14 / 87 Added grow zone function installation    and MoreMasters to SkelInit , }
  80. {            which now requires two parameters . The first indicates the number of times to call }
  81. {            MoreMasters . The second is a ProcPtr indicating a user - supplied grow zone}
  82. {            function to be called when memory problems occur .  If nil , no grow zone}
  83. {            function is    installed . ( omh ) }
  84. {7 / 14 / 87 SkelMenu , SkelWindow , and SkelDialog now return zero or non - zero to indicate }
  85. {            failure or success of handler allocation . This could break * all * previous TransSkel }
  86. {            applications ( as will the change to SkelInit , above . Please see the section "How to }
  87. {            adapt old TransSkel to New "    in the manual    for detailed specifications on }
  88. {            how to convert your old programs . TransSkel becomes more memory conscious}
  89. {            with these changes . The functions SkelMenu , SkelWindow , and }
  90. {            SkelDialog are the only routines which actually allocate memory . Since they may be }
  91. {            called at any time , knowing that you have enough memory becomes important . Thus , }
  92. {            these routines return a value to indicate what happened .  If they return zero ,}
  93. {             then memory allocation failed . ( omh ) }
  94. {10 / 21 / 87 Added another parameter to SkelMenu:  drawBar: Boolean . This tells SkelMenu }
  95. {            whether to draw the menu bar after adding the Menu . This is done to eliminate }
  96. {            the menus popping up one at a time . Simply call SkelMenu  with drawBar false}
  97. {            until the    last time you call SkelMenu , then call it (for the last menu )}
  98. {            with drawBar true . ( omh ) }
  99. {10 / 26 / 87 Removed declarations for zoom - in and zoom - out . Added Pascal }
  100. {            changes ( above ) to C version . ( omh )}
  101.  
  102. {    02 / 02 / 88 Merged pd 's 1.04 changes with those of omh, above, to create}
  103. {            release version 2.0 . Fixed bug whereby cmd - key equivalents}
  104. {            for menu selections would execute twice if DA window in front . Thanks }
  105. {            to Don Fredkin and Julian Vrieslander for finding this one , and to Don for the}
  106. {             best fix . ( pd ) }
  107. { 10/28/88 Removed all New Rom calls. }
  108. { 10/28/88 Added support for conditional compilation for dialogs and MPW support.  By setting }
  109. {            the Think_pascal flag to false, TransSkel will run under MPW. Now correctly written for LSP 2.0}
  110.  
  111. {Some fixes done later by Ingemar Ragnemalm. Search for "Ingemar" fo find these.}
  112. {They include some bug fix and hiearcical menu support.}
  113.  
  114. {New fix 18/9-93: Added a filter proc for dialogs, to allow special event processing before}
  115. {an event is passed to IsDialogEvent, when a modeless dialog is in the front. This makes it possible}
  116. {to use user items in modeless dialogs, and to handle return/enter.}
  117.  
  118. unit TransSkel;
  119.  
  120. interface
  121.  
  122. {$SETC supportDialogs:= true }
  123.                     { Set to false to disallow modeless dialog support and save code space }
  124.                 { Set to false to have SkelInit call QuickDraw Inits: InitGraf, InitDialog, etc. }
  125.  
  126. {$IFC UNDEFINED THINK_PASCAL}
  127.  
  128.     uses
  129.         Memtypes, Quickdraw, OSIntf, ToolIntf, PackIntf;
  130. {$ENDC}
  131.  
  132.     procedure SkelInit (noMasters: integer; myGrowZone: ProcPtr);
  133.     procedure SkelMain;
  134.     procedure SkelWhoa;
  135.     procedure SkelClobber;
  136.     function SkelMenu (theMenu: MenuHandle; pSelect: ProcPtr; pClobber: ProcPtr; DrawBar: Boolean): Boolean;
  137.     function SkelHMenu (theMenu: MenuHandle; pSelect: ProcPtr; pClobber: ProcPtr): Boolean; {Added by Ingemar 22/8 -93}
  138.     procedure SkelRmveMenu (theMenu: MenuHandle);
  139.     procedure SkelApple (aboutTitle: Str255; aboutProc: ProcPtr);
  140.     function SkelWindow (theWind: WindowPtr; pMouse, pKey, pUpdate, pActivate, pClose, pClobber, pIdle: ProcPtr; frontOnly: Boolean): Boolean;
  141.     procedure SkelRmveWind (theWind: WindowPtr);
  142. {$IFC supportDialogs }
  143.     function SkelDialog (theDialog: DialogPtr; pEvent, pClose, pClobber, pFilter: ProcPtr): Boolean; {pFilter added by Ingemar 18/9-93}
  144.     procedure SkelRmveDlog (theDialog: DialogPtr);
  145. {$ENDC}
  146.     procedure SkelGrowBounds (theWind: WindowPtr; hLO, vLo, hHi, vHi: integer);
  147.     procedure SkelEventMask (mask: integer);
  148.     procedure SkelGetEventMask (var mask: integer);
  149.     procedure SkelBackground (p: ProcPtr);
  150.     procedure SkelGetBackground (var p: ProcPtr);
  151.     procedure SkelEventHook (p: ProcPtr);
  152.     procedure SkelGetEventHook (var p: ProcPtr);
  153. {$IFC supportDialogs }
  154.     procedure SkelDlogMask (mask: integer);
  155.     procedure SkelGetDlogMask (var mask: integer);
  156. {$ENDC}
  157. {Two new procedures for WNE-support, added by Ingemar 12/11-93}
  158.     procedure SkelSetSleep (newSleep: Longint);
  159.     procedure SkelSetMouseRgn (newMouseRgn: RgnHandle);
  160.  
  161.  
  162. implementation
  163.  
  164.     const
  165.         mBarHeight = 20;    { menu bar height.  All window sizing}
  166.  
  167.         GrowZoneSize = 4000;        {  Size of memory to be freed when GrowZone Proc called }
  168.  
  169.         defaultSleep = 5;        { Added by Ingemar dec -93. SkelInit uses this value for sleep time.}
  170.                             { The programmer can change it with SkelSetSleep }
  171.  
  172. {    This window zooming stuff may need to be removed if you use the new Rom libraries        }
  173. {    if not, then you can add zooming without the overhead of the new Rom libs.  See TrackBox    }
  174. {     routine also.                                                                                            }
  175.  
  176. {    Window and Menu handler types, constants, variables.}
  177.  
  178. {    whList and mhList are the lists of window and menu handlers.}
  179. {    whClobOnRmve and mhClobOnRmve are true if the handler disposal proc}
  180. {    is to be called when a handler is removed.  They are temporarily set}
  181. {    false when handlers are installed for windows or menus that already}
  182. {    have handlers - the old handler is removed WITHOUT calling the}
  183. {    disposal proc.}
  184.  
  185. {    Default lower limits on window sizing of 80 pixels both directions is}
  186. {    sufficient to allow text windows room to draw a grow box and scroll}
  187. {    bars without having the thumb and arrows overlap.  These values may}
  188. {    be changed if such a constraint is undesirable with SkelGrowBounds.}
  189. {    Default upper limits are for the Macintosh, not the Lisa, but are set}
  190. {    per machine in SkelInit.}
  191.  
  192.     type
  193.         WHandlerPtr = ^WHandler;
  194.         WHandlerHnd = ^WHandlerPtr;
  195.         WHandler = record
  196.                 whWind: WindowPtr;    {window/dialog to be handled    }
  197.                 whClobber: ProcPtr;    { data structure disposal proc    }
  198.                 whMouse: ProcPtr;        { mouse-click handler proc        }
  199.                 whKey: ProcPtr;        { key-click handler proc            }
  200.                 whUpdate: ProcPtr;        { update handler proc                }
  201.                 whActivate: ProcPtr;    { activate event handler proc    }
  202.                 whClose: ProcPtr;        { close "event" handler proc        }
  203.                 whIdle: ProcPtr;            { main loop proc                    }
  204. {$IFC supportDialogs }
  205.                 whEvent: ProcPtr;        { dialog event proc                }
  206.                 whFilter: ProcPtr;        { dialog filter proc ADDED BY INGEMAR 18/9 -93}
  207. {$ENDC }
  208.                 whHasGrow: Boolean;    { can window grow?                }
  209.                 whGrow: Rect;            { limits on window sizing        }
  210.                 whSized: Boolean;        { true = window was resized    }
  211.                 whFrontOnly: Boolean;    { true = idle only when active    }
  212.                 whNext: WHandlerHnd;            { next window handler            }
  213.             end;
  214.  
  215.         MHandlerPtr = ^MHandler;
  216.         MHandlerHnd = ^MHandlerPtr;
  217.  
  218.         MHandler = record
  219.                 mhID: integer;                { menu id                                    }
  220.                 mhSelect: ProcPtr;            { item selection handler proc            }
  221.                 mhClobber: ProcPtr;        { menu disposal handler proc            }
  222.                 mhNext: MHandlerHnd;        { next menu handler                        }
  223.             end;
  224.  
  225.     var
  226.         whList: WHandlerHnd;                { list of menu handlers }
  227.         whClobOnRmve: Boolean;
  228.         growRect: Rect;
  229.         mhList: MHandlerHnd;
  230.         mhClobOnRmve: Boolean;
  231.  
  232. {    Variables for default Apple menu handler.  appleID is set to 1 if}
  233. {    SkelApple is called and is the id of the Apple menu, appleAboutProc}
  234. {    is the procedure to execute if there is an About... item and it's}
  235. {    chosen from the Apple menu.  If doAbout is true, then the menu}
  236. {    contains the About... item, otherwise it's just desk accessories.}
  237.  
  238.         appleMenu: MenuHandle;
  239.         appleID: integer;
  240.         appleAboutProc: ProcPtr;
  241.         doAbout: Boolean;
  242.  
  243. {    Miscellaneous}
  244.  
  245. {    screenPort points to the window manager port.}
  246.  
  247. {    doneFlag determines when SkelMain returns.  It is set by calling}
  248. {    SkelWhoa(), which the host does to request a halt.}
  249.  
  250. {    pBkgnd points to a background procedure, to be run during event}
  251. {    processing.  Set it with SkelBackground.  If nil, there's no}
  252. {    procedure.}
  253.  
  254. {    pEvent points to an event-inspecting hook, to be run whenever an}
  255. {    event occurs.  Set it with SkelEventHook.  If nil, there's no}
  256. {    procedure.}
  257.  
  258. {    eventMask controls the event types requested in the GetNextEvent}
  259. {    call in SkelMain.}
  260.  
  261. {    diskInitPt is the location at which the disk initialization dialog}
  262. {    appears, if an uninitialized disk is inserted.}
  263.  
  264.         screenPort: GrafPtr;
  265.         doneFlag: integer;
  266.         pBkgnd: ProcPtr;
  267.         pEvent: ProcPtr;
  268.         eventMask: integer;
  269.         diskInitPt: Point;
  270.  
  271. {Added by Ingemar 12/11-93, for WNE-support:}
  272.         WNEImplemented: Boolean;
  273.         sleepTicks: Longint;
  274.         mouseRgn: rgnHandle;
  275.  
  276. {$IFC supportDialogs }
  277.  
  278. {    Events that are passed to dialogs.  Others are ignored.}
  279. {    Standard mask passes , mousedown, keydown, autokey, update,}
  280. {    activate and null events.  Null events are controlled by bit 0.}
  281.  
  282.         dlogEventMask: integer;
  283. {$ENDC}
  284.         pEventflag: Boolean;
  285.  
  286. {    "caching" global variables.  previous version would search down the window }
  287. {    list for every event it found.  Now, if the event happened to the same window }
  288. {    as last time, GetWDHandler will just do a simple compare }
  289. {    and return the last window handler.  This speeds up multiple window applications }
  290. {    immensely, at only a slight cost when you activate a new window (one }
  291. {    compare!)  If you don't like it, use the old version. }
  292.  
  293.         oldWindow: WindowPtr;
  294.         oldWDHandler: WHandlerHnd;
  295.  
  296. {    Global for built in "Grow Zone" function  }
  297.  
  298.         safetyHandle: Handle;
  299.  
  300.         myDitl: packed array[0..100] of byte;
  301.  
  302. { Rather than including the entire new ROM libraries, with all the other stuff you might not use    }
  303. { I've instead included just the Zoom box stuff here.  Depending on your status, you can either    }
  304. { leave things as they are, and only use zooming from the new Rom libs, or comment out the        }
  305. { calls, and include the new Rom libraries if you want to incorporate other new Rom calls        }
  306.  
  307.  
  308. { -------------------------------------------------------------------- }
  309. {                        Internal (private) Routines                                    }
  310. { -------------------------------------------------------------------- }
  311.  
  312.  
  313.  
  314. {    Get handler associated with user or dialog window.}
  315. {    Return nil if window doesn't belong to any known handler.}
  316. {    This routine is absolutely fundamental to TransSkel.}
  317.  
  318.     function GetWDHandler (theWind: WindowPtr): WHandlerHnd;
  319.  
  320.         var
  321.             h: WHandlerHnd;
  322.     begin
  323.         h := WhList;
  324.         GetWDHandler := nil;
  325.         if theWind = oldWindow then            {  caching code     }
  326.             GetWDHandler := oldWDHandler
  327.         else
  328.             while h <> nil do
  329.                 if h^^.whWind = theWind then
  330.                     begin
  331.                         oldWindow := theWind;            { Load in new values for new window }
  332.                         oldWDHandler := h;
  333.                         GetWDHandler := h;
  334.                         h := nil;
  335.                     end
  336.                 else
  337.                     h := WHandlerHnd(h^^.whNext);
  338.     end;
  339.  
  340. { Get Handler associated with user window.  Return nil if window doesn't}
  341. {  have a Handler. }
  342.  
  343.     function GetWHandler (theWind: WindowPtr): WHandlerHnd;
  344.  
  345.         var
  346.             h: WHandlerHnd;
  347.             myPeek: WindowPeek;
  348.  
  349.     begin
  350. {BUG FIXED by Ingemar 19/9-93. This function retured garbage when passed a dialog}
  351.         h := GetWDHandler(theWind);
  352.         myPeek := WindowPeek(theWind);
  353.         GetWHandler := nil; {default, moved up by Ingemar}
  354.         if h <> nil then
  355.             begin
  356.                 if mypeek^.windowKind <> dialogKind then
  357.                     GetWHandler := h;
  358.             end;
  359.     end;
  360.  
  361. {$IFC supportDialogs }
  362.  
  363. {    Get handler associated with dialog window.}
  364. {    Return nil if window doesn't belong to any known handler.}
  365.  
  366.     function GetDHandler (theDialog: WindowPtr): WHandlerHnd;
  367.  
  368.         var
  369.             h: WHandlerHnd;
  370.             myPeek: WindowPeek;
  371.  
  372.     begin
  373. {BUG FIXED by Ingemar 19/9-93. This function retured garbage when passed a non-dialog}
  374.         h := GetWDHandler(theDialog);
  375.         myPeek := WindowPeek(theDialog);
  376.         GetDHandler := nil; {default - moved up by Ingemar}
  377.         if h <> nil then
  378.             begin
  379.                 if mypeek^.windowKind = dialogKind then
  380.                     GetDHandler := h;
  381.             end;
  382.     end;
  383. {$ENDC}
  384.  
  385. {Two new routines added by Ingemar 12/11-93 for WNE support:}
  386.     procedure SkelSetSleep (newSleep: Longint);
  387.     begin
  388.         sleepTicks := newSleep;
  389.     end;
  390.     procedure SkelSetMouseRgn (newMouseRgn: RgnHandle);
  391.     begin
  392.         mouseRgn := newMouseRgn;
  393.     end;
  394.  
  395. {The following procedures are Pascal "glue" that allows Pascal to call a Procedure    }
  396. { from a ProcPtr.  It is similar to (*p) () construct used in the C dialect.  Different    }
  397. { procedures are necessary for the reason of Pascal's strongly typed parameter        }
  398. { list.  Fortunately, there are not too many calls which use different param lists        }
  399.  
  400.     procedure callpMouse (thePoint: Point; theTime: longint; theMods: integer; myProc: ProcPtr);
  401.  
  402.     inline
  403.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  404.         $4e90;
  405.  
  406.     procedure callpKey (theChar: char; theMods: integer; myProc: ProcPtr);
  407.  
  408.     inline
  409.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  410.         $4e90;
  411.  
  412.     procedure callpEvent (theitem: integer; var theEvent: EventRecord; myProc: ProcPtr);
  413.  
  414.     inline
  415.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  416.         $4e90;
  417.  
  418.     function callotherEvent (var theEvent: EventRecord; myProc: ProcPtr): Boolean;
  419.  
  420.     inline
  421.         $205f, $4e90;
  422.  
  423.  
  424.     procedure callpBoolean (myBool: Boolean; myProc: ProcPtr);
  425.  
  426. { Two calls use Booleans as one parameter arguments.  This procedure handles    }
  427. { both of them.                                                                            }
  428.  
  429.     inline
  430.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  431.         $4e90;
  432.  
  433.     procedure callpInt (myInt: integer; myProc: ProcPtr);
  434.  
  435. { Two calls use Booleans as one parameter arguments.  This procedure handles    }
  436. { both of them.                                                                            }
  437.  
  438.     inline
  439.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  440.         $4e90;
  441.  
  442.     procedure callpMenu (myMenu: MenuHandle; myProc: ProcPtr);
  443.  
  444. { Handle removeal of menus.     }
  445.  
  446.     inline
  447.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  448.         $4e90;
  449.  
  450.     procedure callpnoarg (myProc: ProcPtr);
  451.  
  452. { For all the Procedures that are called with no arguments                            }
  453.  
  454.     inline
  455.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  456.         $4e90;
  457.  
  458.  
  459. {ADDED BY INGEMAR 18/9-93 to support new dialog filters}
  460. {IS IT POSSIBLE to use this for functions?}
  461.     procedure callpfilter (theDialog: DialogPtr; var theEvent: EventRecord; var result: Boolean; myProc: ProcPtr);
  462.     inline
  463.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  464.         $4e90;
  465.  
  466. {    General menu-handler.  Just passes selection to the handler's}
  467. {    select routine.  If the select routine is nil, selecting items from}
  468. {    the menu is a nop.}
  469.  
  470.     procedure DoMenuCommand (command: longint);
  471.  
  472.         var
  473.             menu: integer;
  474.             item: integer;
  475.             mh: MHandlerHnd;
  476.             p: ProcPtr;
  477.  
  478.     begin
  479.         menu := HiWord(command);
  480.         item := LoWord(command);
  481.         mh := mhList;
  482.         while (mh <> nil) do
  483.             begin
  484.                 p := mh^^.mhSelect;
  485.                 if ((menu = mh^^.mhID) and (p <> nil)) then
  486.                     begin
  487.                         callpInt(item, p);
  488.                         mh := nil;
  489.                     end
  490.                 else
  491.                     mh := mh^^.mhNext;
  492.             end;
  493.         HiliteMenu(0);
  494.     end;
  495.  
  496. {    Apple menu handler}
  497.  
  498. {    DoAppleItem:  If the first item was chosen, and there's an "About..."}
  499. {    item, call the procedure associated with it (if not nil).  If there}
  500. {    is no "About..." item or the item was not the first one, then open}
  501. {    the associated desk accessory.  The port is saved and restored}
  502. {    because OpenDeskAcc does not always preserve it correctly.}
  503.  
  504. {    DoAppleClobber disposes of the Apple menu.}
  505.  
  506.     procedure DoAppleItem (item: integer);
  507.  
  508.         var
  509.             curPort: GrafPtr;
  510.             str: Str255;
  511.             ignore: integer;
  512.             h: Handle;
  513.  
  514.     begin
  515.         if doAbout and (item = 1) then
  516.             begin
  517.                 if appleAboutProc <> nil then
  518.                     callpnoarg(appleAboutProc);
  519.             end
  520.         else
  521.             begin
  522.                 GetPort(curPort);
  523.                 GetItem(appleMenu, item, str);
  524.                 SetResLoad(false);
  525.                 h := GetNamedResource('DRVR', str);
  526.                 SetResLoad(true);
  527.                 if h <> nil then
  528.                     begin
  529.                         ResrvMem(SizeResource(h) + $1000);
  530.                         ignore := OpenDeskAcc(str);
  531.                     end;
  532.                 SetPort(curPort);
  533.             end;
  534.     end;
  535.  
  536.     procedure DoAppleClobber;
  537.     begin
  538.         DisposeMenu(appleMenu);
  539.     end;
  540.  
  541. { --------------------------------------------------------------------     }
  542. {                        Window-handler routing routines                            }
  543. {                                                                                        }
  544. {    Each routine sets the port to the handler's window before executing        }
  545. {    the handler procedure.                                                            }
  546. { --------------------------------------------------------------------     }
  547.  
  548.  
  549. {    Pass local mouse coordinates, click time, and the modifiers flag}
  550. {    word to the handler.  Should not be necessary to set the port, as}
  551. {       the click is passed to the active window's hander. }
  552.  
  553.     procedure DoMouse (h: WHandlerHnd; theEvent: EventRecord);
  554.  
  555.         var
  556.             p: ProcPtr;
  557.             thePt: Point;
  558.  
  559.     begin
  560.         if (h <> nil) then
  561.             begin
  562.                 p := h^^.whMouse;
  563.                 if p <> nil then
  564.                     begin
  565.                         thePt := theEvent.where;
  566.                         GlobalToLocal(thePt);
  567.                         callpMouse(thePt, theEvent.when, theEvent.modifiers, p);
  568.                     end;
  569.             end;
  570.     end;
  571.  
  572. {    Pass the character and the modifiers flag word to the handler.}
  573. {    Should not be necessary to set the port, as the  click is passed to the}
  574. {    active window's handler. }
  575.  
  576.     procedure DoKey (h: WHandlerHnd; ch: char; mods: integer);
  577.         var
  578.             p: ProcPtr;
  579.  
  580.     begin
  581.         if h <> nil then
  582.             begin
  583.                 p := h^^.whKey;
  584.                 if p <> nil then
  585.                     callpKey(ch, mods, p);
  586.             end;
  587.     end;
  588.  
  589. {    Call the window updating procedure, passing to it an indicator whether the}
  590. {    window has been resized or not.  Then clear the flag, assuming the update}
  591. {    proc took whatever action was necessary to respond to resizing.}
  592. {}
  593. {    If the handler doesn't have any update proc, the Begin/EndUpdate stuff}
  594. {    is still done, to clear the update region.  Otherwise the Window Manager }
  595. {    will keep generating update events for the window, stalling updates of}
  596. {    other windows.    }
  597.  
  598. {    Make sure to save and restore the port, as it's not always the active window}
  599. {    that's updated.    }
  600.  
  601.     procedure DoUpdate (h: WHandlerHnd);
  602.  
  603.         var
  604.             rh: WhandlerHnd;
  605.             p: ProcPtr;
  606.             updPort, tmpPort: GrafPtr;
  607.  
  608.     begin
  609.         rh := h;
  610.         if rh <> nil then
  611.             begin
  612.                 GetPort(tmpPort);
  613.                 updPort := rh^^.whWind;
  614.                 SetPort(updPort);
  615.                 BeginUpdate(updPort);
  616.                 p := rh^^.whUpdate;
  617.                 if p <> nil then
  618.                     begin
  619.                         callpBoolean(rh^^.whSized, p);
  620.                         rh^^.whSized := false;
  621.                     end;
  622.                 EndUpdate(updPort);
  623.                 SetPort(tmpPort);
  624.             end;
  625.     end;
  626.  
  627. {    Pass activate/deactivate notification to handler.  On activate, set the port to}
  628. {    the window coming active    }
  629.  
  630.     procedure DoActivate (h: WHandlerHnd; active: Boolean);
  631.  
  632.         var
  633.             p: ProcPtr;
  634.  
  635.     begin
  636.         if h <> nil then
  637.             begin
  638.                 if active then
  639.                     SetPort(h^^.whWind);
  640.                 p := h^^.whActivate;
  641.                 if p <> nil then
  642.                     callpBoolean(active, p);
  643.             end
  644.     end;
  645.  
  646. {    Execute a window handler's close proc.  The close box for handlers}
  647. {    for temp windows that want to remove themselves when the window}
  648. {    is closed can call SkelRmveWind to dispose of the window}
  649. {    and remove the handler from the window handler list.  Thus, windows}
  650. {    may be dynamically created and destroyed without filling up the}
  651. {    handler list with a bunch of invalid handlers.}
  652.  
  653. {    If the handler doesn't have a close proc, just hide the window.}
  654. {    The host should provide some way of reopening the window (perhaps}
  655. {    a menu selection).  Otherwise the window will be lost from user}
  656. {    control if it is hidden, since it won't receive user-initiated events.}
  657.  
  658. {    Since the close box of only the active window may be clicked, it}
  659. {    is not necessary to set the port . }
  660.  
  661. {    This is called both for regular and dialog windows.}
  662.  
  663.     procedure DoClose (h: WHandlerHnd);
  664.  
  665.         var
  666.             rh: WHandlerHnd;
  667.             p: ProcPtr;
  668.     begin
  669.         rh := h;
  670.         if rh <> nil then
  671.             begin
  672.                 p := rh^^.whClose;
  673.                 if (p <> nil) then
  674.                     callpnoarg(p)
  675.                 else
  676.                     HideWindow(rh^^.whWind);
  677.             end;
  678.     end;
  679.  
  680. {    Execute a window Handler's clobber proc.  This is called both for regular and dialog windows.}
  681. {    Must save, set and restore port, since any window (not just active one) may be clobbered    }
  682. {    at any time.}
  683. {}
  684. {    Don't need to check whether handler is nil, as in other handler procedures, since this is only}
  685. {    called by SkelRmveWind with a known valid handler.     }
  686.  
  687.     procedure DoClobber (h: WHandlerHnd);
  688.  
  689.         var
  690.             p: ProcPtr;
  691.             curPort: Grafptr;
  692.     begin
  693.         if (h <> nil) then
  694.             begin
  695.                 GetPort(curPort);
  696.                 SetPort(h^^.whWind);
  697.                 p := h^^.whClobber;
  698.                 if p <> nil then
  699.                     callpnoarg(p);
  700.                 SetPort(curPort);
  701.             end;
  702.     end;
  703.  
  704. {$IFC supportDialogs }
  705.  
  706. {    Handle event if it's for a dialog.  The event must be one of}
  707. {    those that is passed to dialogs according to dlogEventMask.}
  708. {    This mask can be set so that disk-inserts, for instance, don't}
  709. {    get eaten up.}
  710.  
  711.     function DoDialog (theEvent: EventRecord): Boolean;
  712.  
  713.         var
  714.             dh: WHandlerHnd;
  715.             theDialog: DialogPtr;
  716.             myDPeek: DialogPeek;
  717.             what: integer;
  718.             item: integer;
  719.             tmpPort: GrafPtr;
  720.             ignore: Boolean;
  721.             testme: longint;
  722.             hasfilter, filtered: Boolean;
  723.  
  724.     begin
  725.  
  726. {    handle command keys before they get to IsDialogEvent}
  727.  
  728.         what := theEvent.what;
  729.         testme := BitShift(longint(1), what);
  730.         testme := BitAnd(testme, longint(dlogEventMask));
  731.         if (((what = keydown) or (what = autokey)) and (BitAnd(theEvent.modifiers, cmdkey) <> 0)) then
  732. { Bugfix by Ingemar above! }
  733.             begin
  734.                 DoMenuCommand(MenuKey(Char(BitAnd(theEvent.message, charCodeMask))));
  735.                 DoDialog := true;
  736.             end
  737. {Filter procedure, Added by Ingemar 18/9 -93:}
  738.         else
  739.             begin
  740. {Kolla om FrontWindow har whFilter!}
  741.                 dh := WHandlerHnd(GetDHandler(FrontWindow));
  742.                 filtered := false;
  743.                 hasfilter := dh <> nil;
  744.                 if hasfilter then
  745.                     hasfilter := dh^^.whFilter <> nil;
  746.                 if hasfilter then
  747.                     callpfilter(FrontWindow, theEvent, filtered, dh^^.whFilter);
  748.                 DoDialog := filtered;
  749.                 if not filtered then
  750. {end of filter proc handling}
  751. {else}
  752.                     if testme > 0 then
  753.                         if IsDialogEvent(theEvent) then
  754.                             begin
  755.                                 if DialogSelect(theEvent, theDialog, item) then
  756.                                     begin
  757.                                         dh := WHandlerHnd(GetDHandler(theDialog));
  758.                                         if (dh <> nil) then
  759.                                             if (dh^^.whEvent <> nil) then
  760.                                                 begin
  761.                                                     GetPort(tmpPort);
  762.                                                     SetPort(theDialog);
  763.                                                     callpEvent(item, theEvent, dh^^.whEvent);
  764.                                                     SetPort(tmpPort);
  765.                                                 end;
  766.                                     end;
  767.                                 DoDialog := true;
  768.                             end
  769.                         else
  770.                             DoDialog := false;
  771.             end; {to match begin added with filter above}
  772.     end;
  773. {$ENDC}
  774.  
  775. { -------------------------------------------------------------------- }
  776. {                            Event-handling routines                        }
  777. { -------------------------------------------------------------------- }
  778.  
  779. {    Have either sized or zoomed the window.  Invalidate it to force}
  780. {    an update and set the 'resized' flag in the window handler true.}
  781. {    The port is assumed to be set to the port that changed size.        }
  782.  
  783.     procedure TriggerUpdate (h: WHandlerHnd; thePort: GrafPtr);
  784.  
  785.     begin
  786.         InvalRgn(thePort^.visRgn);
  787. {Changed by Ingemar 1/4-94. Was: InvalRect(thePort^.portRect);}
  788. {This is ok for rectangular windows, but all windows aren't rectangular!}
  789.         if (h <> nil) then
  790.             begin
  791.                 h^^.whSized := true;
  792.             end;
  793.     end;
  794.  
  795. {    Size a window.  If the window has a handler, use the grow limits}
  796. {    in the handler record, otherwise use the defaults.}
  797.  
  798. {    The portRect is invalidated to force an update event.    The handler's}
  799. {    update procedure should check the parameter passed to it to check}
  800. {    whether the window has changed size, if it needs to adjust itself to}
  801. {    the new size.  THIS IS A CONVENTION.  Update procs must notice grow}
  802. {    "events", there is no procedure specifically for such events.}
  803.  
  804. {    The clipping rectangle is not reset.  If the host application}
  805. {    keeps the clipping set equal to the portRect or something similar,}
  806. {    then it will have to arrange to treat window growing with more}
  807. {    care.}
  808. {}
  809. {    Since the grow region of only the active window may be clicked, it should}
  810. {    not be necessary to set the port.}
  811.  
  812.     procedure DoGrow (h: WHandlerHnd; thePort: GrafPtr; StartPt: Point);
  813.  
  814.         var
  815.             r: Rect;
  816.             growRes: longint;
  817.  
  818.     begin
  819.         if (h <> nil) then
  820.             begin
  821.                 r := h^^.whGrow;
  822.             end
  823.         else
  824.             r := growRect;
  825.         growRes := GrowWindow(thePort, startPt, r);
  826.         if growRes <> 0 then
  827.             begin
  828.                 SizeWindow(thePort, LoWord(growRes), HiWord(growRes), false);
  829.                 TriggerUpdate(h, thePort);
  830.             end;
  831.     end;
  832.  
  833.  
  834. {    Zoom the current window.  Very similar to DoGrow}
  835. {    Since the zoombox of only the active window may be clicked, it should not be necessary}
  836. {    to set the port.    }
  837.  
  838.     procedure DoZoom (h: WHandlerHnd; thePort: GrafPtr; partcode: integer);
  839.  
  840.     begin
  841.         ZoomWindow(thePort, partcode, false);
  842.         TriggerUpdate(h, thePort);
  843.     end;
  844.  
  845. {    General event handler}
  846.  
  847.     procedure DoEvent (theEvt: Eventrecord);
  848.  
  849.         var
  850.             theEvent: EventRecord;
  851.             evtPt: Point;
  852.             evtPort: GrafPtr;
  853.             evtPart: integer;
  854.             evtChar: char;
  855.             evtMods: integer;
  856.             h: WHandlerHnd;
  857.             r: Rect;
  858.             ignore: integer;
  859.  
  860.     begin
  861.         theEvent := theEvt;
  862. {$IFC supportDialogs }
  863.         if not (DoDialog(theEvent)) then
  864. {$ENDC}
  865.             begin
  866.                 evtPt := theEvent.where;
  867.                 case theEvent.what of
  868.                     nullEvent: 
  869.                         ;
  870.  
  871. {    Mouse click.  Get the window that the click occurred in, and the}
  872. {    part of the window.    Get WDHandler is called here, not GetWHandler, since}
  873. {    we need the handler for a window which might turn out to be a dialog window,}
  874. {    e.g., if the click is in a close box.}
  875.  
  876.                     mouseDown: 
  877.                         begin
  878.                             evtPart := FindWindow(evtPt, evtPort);
  879.                             h := GetWDHandler(evtPort);
  880.                             case evtPart of
  881.  
  882. {    Click in a desk accessory window.  Pass back to the system.}
  883.  
  884.                                 inSysWindow: 
  885.                                     SystemClick(theEvent, evtPort);
  886.  
  887. {    Click in menu bar.  Track the mouse and execute selected command,}
  888. {    if any.}
  889.  
  890.                                 inMenuBar: 
  891.                                     DoMenuCommand(MenuSelect(evtPt));
  892.  
  893. {    Click in grow box.  Resize window.}
  894.  
  895.                                 inGrow: 
  896.                                     DoGrow(h, evtPort, evtPt);
  897.  
  898. {    Click in title bar.  Drag the window around.  Leave at least}
  899. {    4 pixels visible in both directions.    Bug fix: The window, if not front, is}
  900. {    selected first to make sure it's at least activated (unless the command key is down - see Inside}
  901. {    Macintosh).  DragWindow seems to call StillDown first, so that clicks in drag regions while}
  902. {    machine is busy don't otherwise bring window to front if the mouse is already up by the time}
  903. {    DragWindow is called.}
  904.  
  905. {BUG??? Where is evtmods assigned???}
  906.  
  907.                                 inDrag: 
  908.                                     begin
  909.                                         if (evtPort <> FrontWindow) and (BitAnd(evtmods, cmdKey) = 0) then
  910.                                             SelectWindow(evtPort);
  911.                                         r := screenPort^.portRect;
  912.                                         r.top := r.top + mBarHeight;        { Skip down past menu bar    }
  913.                                         InsetRect(r, 4, 4);
  914.                                         DragWindow(evtPort, evtPt, r);
  915.                                     end;
  916.  
  917. {    Click in close box.  Call the close proc if the window has one.}
  918.  
  919.                                 inGoAway: 
  920.                                     if (TrackGoAway(evtPort, evtPt)) then
  921.                                         DoClose(GetWDHandler(evtPort));
  922.  
  923. {    Click in content region.  If the window wasn't frontmost (active),}
  924. {    just select it, otherwise pass the click to the window's mouse}
  925. {    click handler.}
  926.  
  927.                                 inContent: 
  928.                                     if (evtPort <> FrontWindow) then
  929.                                         SelectWindow(evtPort)
  930.                                     else
  931.                                         DoMouse(h, theEvent);
  932.  
  933. {    Click in zoom box.  Track the click and then zoom the window if}
  934. {    necessary}
  935.  
  936.                                 inZoomin, inZoomOut: 
  937.                                     if (TrackBox(evtPort, evtPt, evtPart)) then
  938.                                         DoZoom(h, evtport, evtPart);
  939.                                 otherwise
  940.                                     ;
  941.                             end;{mousedown}
  942.                         end;
  943.  
  944. {    Key event.  If the command key was down, process as menu item}
  945. {    selection, otherwise pass the character and the modifiers flags}
  946. {    to the active window's key handler.}
  947.  
  948. {    If dialogs are supported, there's no check for command-key}
  949. {    equivalents, since that would have been checked in DoDialog.}
  950.  
  951.                     keydown, autokey: 
  952.                         begin
  953.                             evtChar := char(BitAnd(theEvent.message, charCodeMask));
  954.                             evtMods := theEvent.modifiers;
  955.                             if BitAnd(evtMods, cmdKey) > 0 then
  956.                                 DoMenuCommand(menuKey(evtChar))
  957.                             else
  958.                                 DoKey(GetWHandler(FrontWindow), evtChar, evtMods);
  959.                         end;
  960.  
  961. {    Update a window.}
  962.  
  963.                     updateEvt: 
  964.                         DoUpdate(GetWHandler(WindowPtr(theEvent.message)));
  965.  
  966. {    Activate or deactivate a window.}
  967.  
  968.                     activateEvt: 
  969.                         DoActivate(GetWHandler(WindowPtr(theEvent.message)), (BitAnd(theEvent.modifiers, activeFlag) <> 0));
  970.  
  971. {    handle inserts of uninitialized disks}
  972.  
  973.                     diskEvt: 
  974.                         if (HiWord(theEvent.message) <> noErr) then
  975.                             begin
  976.                                 DILoad;
  977.                                 ignore := DIBadMount(diskInitPt, theEvent.message);
  978.                                 DIUnload;
  979.                             end;
  980.                     otherwise
  981.                 end;
  982.             end;
  983.     end;
  984.  
  985. { -------------------------------------------------------------------- }
  986. {                        Interface (public) Routines                        }
  987. { -------------------------------------------------------------------- }
  988.  
  989.  
  990. {    Initialize the various Macintosh Managers.}
  991. {    Set default upper limits on window sizing.}
  992. {    FlushEvents does NOT toss disk insert events, so that disks}
  993. {    inserted while the application is starting up don't result}
  994. {    in dead drives.}
  995. {    NoMasters is the number of times to call MoreMasters.  gzProc is the address of a user - provided}
  996. {    grow zone function procedure to call if memory gets tight.  Pass nil if none to be used.    }
  997.  
  998.     procedure SkelInit;
  999.         var
  1000.             i: integer;
  1001.  
  1002.     begin
  1003.  
  1004. { For non-Lightspeed Pascal users, the following inits are included as a compile time option, }
  1005. {  See the $SETC definition at the beginning of the unit.  }
  1006.  
  1007. {$IFC UNDEFINED THINK_PASCAL }
  1008.  
  1009.         InitGraf(@thePort);
  1010.         InitFonts;
  1011.         InitWindows;
  1012.         InitMenus;
  1013.         TEInit;
  1014.         InitDialogs(nil);
  1015.         MaxApplZone;
  1016. {$ENDC}
  1017.  
  1018.         FlushEvents(everyEvent - diskMask, 0);
  1019.         for i := 1 to noMasters do
  1020.             MoreMasters;
  1021.         if myGrowZone <> nil then
  1022.             SetGrowZone(myGrowZone);
  1023.  
  1024.         InitCursor;
  1025.  
  1026. {Added by Ingemar 12/11-93, for WNE-support:}
  1027.         WNEImplemented := NGetTrapAddress($60, ToolTrap) <> NgetTrapAddress($9F, ToolTrap);
  1028.         SkelSetSleep(defaultSleep);
  1029.         SkelSetMouseRgn(nil);
  1030. {End WNE-support}
  1031.  
  1032.         whList := nil;
  1033.         whClobOnRmve := true;
  1034.         SetRect(growRect, 80, 80, 512, 342 - mBarHeight);
  1035.         mhList := nil;
  1036.         mhClobOnRmve := true;
  1037.         appleID := 0;
  1038.         appleAboutProc := nil;
  1039.         doAbout := false;
  1040.         doneflag := 0;
  1041.         pBkgnd := nil;
  1042.         pEvent := nil;
  1043.         pEventflag := false;
  1044.         eventmask := everyEvent;
  1045.         diskInitPt.v := 120;
  1046.         diskInitPt.h := 100;
  1047. {$IFC supportDialogs }
  1048.         dlogEventMask := $16f;
  1049. {$ENDC}
  1050.  
  1051. {    Set upper limits of window sizing to machine screen size.  Allow}
  1052. {    for the menu bar.}
  1053.  
  1054.         GetWMgrPort(screenport);
  1055.         growRect.right := screenPort^.portRect.right;
  1056.         growRect.bottom := screenPort^.portRect.bottom - mBarHeight;
  1057.  
  1058. {    Set caching global variables to nil }
  1059.  
  1060.         oldWindow := nil;
  1061.         oldWDHandler := nil;
  1062.     end;
  1063.  
  1064. {    Main loop.}
  1065.  
  1066. {    Task care of DA's with SystemTask.}
  1067. {    Run background task if there is one.}
  1068. {    If there is an event, check for an event hook.  If there isn't}
  1069. {    one defined, or if there is but it returns false, call the}
  1070. {    general event handler.  (Hook returns true if TransSkel should}
  1071. {    ignore the event.)}
  1072. {    If no event, call the "no-event" handler for the front window and for}
  1073. {    any other windows with idle procedures that are always supposed}
  1074. {    to run.  This is done in such a way that it is safe for idle procs}
  1075. {    to remove the handler for their own window if they want (unlikely,}
  1076. {    but...)  This loop doesn't check whether the window is really}
  1077. {    a dialog window or not, but it doesn't have to, because such}
  1078. {    things always have a nil idle proc.}
  1079. {    }
  1080. {    doneFlag is reset upon exit.  This allows it to be called}
  1081. {    repeatedly, or recursively.}
  1082.  
  1083. {    Null events are looked at (in SkelMain)}
  1084. {    and passed to the event handler.  This is necessary to make sure}
  1085. {    DialogSelect gets called repeatedly, or the caret won't blink if}
  1086. {    a dialog has any editText items.  Null events are not passed to any event-inspecting hook that may}
  1087. {    be installed.}
  1088.  
  1089.     procedure SkelMain;
  1090.  
  1091.         var
  1092.             theEvent: EventRecord;
  1093.             wh, wh2: WHandlerHnd;
  1094.             w: WindowPtr;
  1095.             haveEvent, testpevent, testbool: Boolean;
  1096.             tmpPort: GrafPtr;
  1097.             p: ProcPtr;
  1098.  
  1099.     begin
  1100.         while (doneFlag = 0) do
  1101.             begin
  1102.                 if WNEImplemented then {Added by Ingemar 12/11-93, for WNE-support:}
  1103.                     begin
  1104.                         if (pBkgnd <> nil) then
  1105.                             callpnoarg(pBkgnd);
  1106.                         haveEvent := WaitNextEvent(eventMask, theEvent, sleepTicks, mouseRgn);
  1107.                     end
  1108.                 else
  1109.                     begin {Old way:}
  1110.                         SystemTask;
  1111.                         if (pBkgnd <> nil) then
  1112.                             callpnoarg(pBkgnd);
  1113.                         haveEvent := GetNextEvent(eventMask, theEvent);
  1114.                     end;
  1115.  
  1116.                 if (pEvent <> nil) then
  1117.                     testpevent := CallotherEvent(theEvent, pEvent)
  1118.                 else
  1119.                     testpevent := false;
  1120. { following line fixed from version 1.02 & 1.03     [but was still buggy! /Ingemar]}
  1121. {$IFC supportDialogs }
  1122.                 if (pEvent = nil) or (testpevent = false) then {haveEvent and <- This is wrong for modeless dialogs according to page 416? Changed by Ingemar 6/8 -93}
  1123. {$ELSEC}
  1124.                     if haveEvent and ((pEvent = nil) or (testpevent = false)) then { Old line. IFC'ad by Ingemar 6/8 -93}
  1125. {$ENDC}
  1126.                         DoEvent(theEvent);
  1127.                 if not haveEvent then
  1128.                     begin
  1129.                         wh := whList;
  1130.                         GetPort(tmpPort);
  1131.                         while (wh <> nil) do
  1132.                             begin
  1133.                                 wh2 := wh^^.whNext;
  1134.                                 w := wh^^.whWind;
  1135.                                 if ((w = FrontWindow) or not wh^^.whFrontOnly) then
  1136.                                     begin
  1137.                                         SystemTask;
  1138.                                         if (wh^^.whIdle <> nil) then
  1139.                                             begin
  1140.                                                 SetPort(wh^^.whWind);
  1141.                                                 p := wh^^.whIdle;
  1142.                                                 if (p <> nil) then
  1143.                                                     callpnoarg(p);
  1144.                                             end;
  1145.                                     end;
  1146.                                 wh := wh2;
  1147.                             end;
  1148.                         SetPort(tmpPort);
  1149.                     end;
  1150.             end;
  1151.         doneFlag := 0;
  1152.     end;
  1153.  
  1154. {    Tell SkelMain to stop}
  1155.  
  1156.     procedure SkelWhoa;
  1157.     begin
  1158.         doneFlag := 1;
  1159.     end;
  1160.  
  1161. {    Clobber all the menu, window and dialog handlers}
  1162.  
  1163.     procedure SkelClobber;
  1164.  
  1165.  
  1166.     begin
  1167.         oldWDHandler := nil;
  1168.         oldWindow := nil;
  1169.         while (whList <> nil) do
  1170.             begin
  1171.                 SkelRmveWind(whList^^.whWind);
  1172.             end;
  1173.         while (mhList <> nil) do
  1174.             begin
  1175.                 SkelRmveMenu(GetMHandle(mhList^^.mhID));
  1176.             end;
  1177.     end;
  1178.  
  1179. { -------------------------------------------------------------------- }
  1180. {                        Menu-handler interface routines                            }
  1181. { -------------------------------------------------------------------- }
  1182.  
  1183.  
  1184.  
  1185.  
  1186. {    Install handler for a menu.  Remove any previous handler for it.}
  1187. {    Pass the following parameters:}
  1188.  
  1189. {    theMenu    Handle to the menu to be handled.  Must be created by host.}
  1190. {    pSelect    Proc that handles selection of items from menu.  If this is}
  1191. {            nil, the menu is installed, but nothing happens when items}
  1192. {            are selected from it.}
  1193. {    pClobber Proc for disposal of handler's data structures.  Usually}
  1194. {            nil for menus that remain in menu bar until program}
  1195. {            termination.}
  1196.  
  1197. {    The menu is installed and drawn in the menu bar.}
  1198.  
  1199. {     Return false if no handler could be allocated, true if successful. }
  1200.  
  1201.     function CommonSkelMenu (theMenu: MenuHandle; pSelect: ProcPtr; pClobber: ProcPtr): Boolean;
  1202.         var
  1203.             mh: MHandlerHnd;
  1204.             myHand: Handle;
  1205.     begin
  1206.         mhClobOnRmve := false;
  1207.         SkelRmveMenu(theMenu);
  1208.         mhClobOnRmve := true;
  1209.         myHand := NewHandle(Sizeof(MHandler));
  1210.         CommonSkelMenu := false;
  1211.         if myHand <> nil then
  1212.             begin
  1213.                 CommonSkelMenu := true;                    { show we really got the memory }
  1214.                 mh := MHandlerHnd(myHand);
  1215.                 mh^^.mhNext := mhList;
  1216.                 mhList := MHandlerHnd(myHand);
  1217.                 mh^^.mhID := theMenu^^.menuID;    { get menu id number }
  1218.                 mh^^.mhSelect := pSelect;            { install selection handler }
  1219.                 mh^^.mhClobber := pClobber;        { install disposal handler }
  1220.             end;
  1221.     end;
  1222.  
  1223. {  Install handler for a normal menu }
  1224.  
  1225.     function SkelMenu;
  1226.         var
  1227.             success: Boolean;
  1228.     begin
  1229.         success := CommonSkelMenu(theMenu, pSelect, pClobber);
  1230.         SkelMenu := success;
  1231.         if success then
  1232.             begin
  1233.                 InsertMenu(theMenu, 0);            { put menu at end of menu bar }
  1234.                 if DrawBar then
  1235.                     DrawMenuBar;
  1236.             end;
  1237.     end;
  1238.  
  1239. {    Install handler for a hiearcical menu.  Almost same as above.}
  1240. {    Added by Ingemar 22/8 -93}
  1241.  
  1242.     function SkelHMenu;
  1243.         var
  1244.             success: Boolean;
  1245.     begin
  1246.         success := CommonSkelMenu(theMenu, pSelect, pClobber);
  1247.         SkelHMenu := success;
  1248.         if success then
  1249.             begin
  1250.                 InsertMenu(theMenu, -1);            { put menu at end of menu bar }
  1251.             end;
  1252.     end;
  1253.  
  1254.  
  1255. {    Remove a menu handler.  This calls the handler's disposal routine}
  1256. {    and then takes the handler out of the handler list and disposes}
  1257. {    of it.}
  1258.  
  1259. {    Note that the menu MUST be deleted from the menu bar before calling}
  1260. {    the clobber proc, because the menu bar will end up filled with}
  1261. {    garbage if the menu was allocated with NewMenu (see discussion of}
  1262. {    DisposeMenu in Menu Manager section of Inside Macintosh).}
  1263.  
  1264.     procedure SkelRmveMenu;
  1265.  
  1266.         var
  1267.             mID: integer;
  1268.             h, h2: MHandlerHnd;
  1269.             p: ProcPtr;
  1270.             returnflag: Boolean;
  1271.  
  1272.     begin
  1273.         mID := theMenu^^.menuID;
  1274.         returnflag := false;
  1275.         if mhlist <> nil then
  1276.             begin
  1277.                 if mhList^^.mhID = mID then
  1278.                     begin
  1279.                         h2 := mhlist;
  1280.                         mhList := h2^^.mhNext;
  1281.                     end
  1282.                 else
  1283.                     begin
  1284.                         h := mhList;
  1285.                         while (h <> nil) and not returnflag do
  1286.                             begin
  1287.                                 h2 := h^^.mhNext;
  1288.                                 if (h2 = nil) then
  1289.                                     begin
  1290.                                         h := nil;
  1291.                                         returnflag := true;
  1292.                                     end
  1293.                                 else if h2^^.mhID = mID then
  1294.                                     begin
  1295.                                         h^^.mhNext := h2^^.mhNext;
  1296.                                         h := nil;
  1297.                                     end;
  1298.                                 if h <> nil then
  1299.                                     h := h2;
  1300.                             end;
  1301.                     end;
  1302.                 if not returnflag then
  1303.                     begin
  1304.                         DeleteMenu(mID);
  1305.                         DrawMenuBar;
  1306.                         p := h2^^.mhClobber;
  1307.                         if mhClobOnRmve and (p <> nil) then
  1308.                             callpMenu(theMenu, p);
  1309.                         DisposHandle(Handle(h2));
  1310.                     end;
  1311.             end;
  1312.     end;
  1313.  
  1314. {    Install a handler for the Apple menu.}
  1315.  
  1316. {    SkelApple is called if TransSkel is supposed to handle the apple}
  1317. {    menu itself.  The title is the title of the first item.  If nil,}
  1318. {    then only desk accessories are put into the menu.  If not nil, then}
  1319. {    the title is entered as the first item, followed by a gray line,}
  1320. {    then the desk accessories.}
  1321.  
  1322. {     SkelApple does not cause the menubar to be drawn, so if the Apple menu is the only menu, }
  1323. {    DrawMenuBar must be called afterward.}
  1324.  
  1325. {    No value is returned, unlike SkelMenu.  It is assumed that SkelApple will be called so early in the}
  1326. {    application that the call to SkelMenu is virtually certain to succeed.  }
  1327.  
  1328.     procedure SkelApple;
  1329.  
  1330.         var
  1331.             appleTitle: Str255;
  1332.             dummy: boolean;
  1333.     begin
  1334.         appleTitle := ' ';
  1335.         appleTitle[1] := char($14);
  1336.         appleID := 1;
  1337.         AppleMenu := NewMenu(appleID, appleTitle);
  1338.         if aboutTitle <> '' then
  1339.             begin
  1340.                 doAbout := true;
  1341.                 AppendMenu(appleMenu, aboutTitle);
  1342.                 AppendMenu(appleMenu, '(-');
  1343.                 AppleAboutProc := aboutProc;
  1344.             end;
  1345.         AddResMenu(appleMenu, 'DRVR');
  1346.         dummy := SkelMenu(appleMenu, @DoAppleItem, @DoAppleClobber, false);
  1347.     end;
  1348.  
  1349. { -------------------------------------------------------------------- }
  1350. {                    Window-handler interface routines                    }
  1351. { -------------------------------------------------------------------- }
  1352.  
  1353.  
  1354.  
  1355. {    Install handler for a window.  Remove any previous handler for it.}
  1356. {    Pass the following parameters:}
  1357.  
  1358. {    theWind    Pointer to the window to be handled.  Must be created by host.}
  1359. {    pMouse    Proc to handle mouse clicks in window.  The proc will be}
  1360. {            passed the point (in local coordinates), the time of the}
  1361. {            click, and the modifier flags word.}
  1362. {    pKey    Proc to handle key clicks in window.  The proc will be passed}
  1363. {            the character and the modifier flags word.}
  1364. {    pUpdate    Proc for updating window.  TransSkel brackets calls to update}
  1365. {            procs with calls to BeginUpdate and EndUpdate, so the visRgn}
  1366. {            is set up correctly.  A flag is passed indicating whether the}
  1367. {            window was resized or not.  BY CONVENTION, the entire portRect}
  1368. {            is invalidated when the window is resized.  That way, the}
  1369. {            handler's update proc can redraw the entire content region}
  1370. {            without interference from BeginUpdate/EndUpdate.  The flag}
  1371. {            is set to false after the update proc is called; the}
  1372. {            assumption is made that it will notice the resizing and}
  1373. {            respond appropriately.}
  1374. {    pActivate Proc to execute when window is activated or deactivated.}
  1375. {            A boolean is passed to it which is true if the window is}
  1376. {            coming active, false if it's going inactive.}
  1377. {    pClose    Proc to execute when mouse clicked in close box.  Useful}
  1378. {            mainly to temp window handlers that want to know when to}
  1379. {            self-destruct (with SkelRmveWind).}
  1380. {    pClobber Proc for disposal of handler's data structures}
  1381. {    pIdle    Proc to execute when no events are pending.}
  1382. {    frontOnly True if pIdle should execute on no events only when}
  1383. {            theWind is frontmost, false if executes all the time.  Note}
  1384. {            that if it always goes, everything else may be slowed down!}
  1385.  
  1386. {    If a particular procedure is not needed (e.g., key events are}
  1387. {    not processed by a handler), pass nil in place of the appropriate}
  1388. {    procedure address.}
  1389.  
  1390. {    Return false  if no handler could be allocated, true if successful.}
  1391.  
  1392.     function SkelWindow;
  1393.  
  1394.         var
  1395.             hHand: WhandlerHnd;
  1396.  
  1397.     begin
  1398.         whClobOnRmve := false;
  1399.         SkelRmveWind(theWind);
  1400.         whClobOnRmve := true;
  1401.  
  1402. {    Get new handler, attach to list of handlers.  It is attached to the beginning of the list, which is simpler;}
  1403. {    the order should be irrelevant to the hose, anyway. }
  1404.  
  1405.         hHand := WHandlerHnd(NewHandle(Sizeof(WHandler)));
  1406.         SkelWindow := false;
  1407.         if hHand <> nil then
  1408.             begin
  1409.                 hHand^^.whNext := whList;
  1410.                 whList := hHand;
  1411.                 with hHand^^ do
  1412.                     begin
  1413.                         SkelWindow := true;                    { Show that we got the memory }
  1414.                         whWind := theWind;
  1415.                         whMouse := pMouse;
  1416.                         whKey := pKey;
  1417.                         whUpdate := pUpdate;
  1418.                         whActivate := pActivate;
  1419.                         whClose := pClose;
  1420.                         whClobber := pClobber;
  1421.                         whIdle := pIdle;
  1422.                         whFrontOnly := frontOnly;
  1423.                         whSized := false;
  1424.                         whGrow := GrowRect;
  1425.                     end;
  1426.             end;
  1427.         SetPort(theWind); {Is this allowed for hidden windows? I thought so, but… /Ingemar, dec 93}
  1428.     end;
  1429.  
  1430. {    Remove a window handler.  This calls the handler's disposal routine}
  1431. {    and then takes the handler out of the handler list and disposes}
  1432. {    of it.}
  1433.  
  1434. {    SkelRmveWind is also called by SkelRmveDlog.}
  1435.  
  1436. {    Note that if the window cache variable is set to the window whose handler is being clobbered, the }
  1437. {    variable must be zeroed.    }
  1438.  
  1439.     procedure SkelRmveWind;
  1440.  
  1441.         var
  1442.             h, h2: WHandlerHnd;
  1443.             returnflag: Boolean;
  1444.  
  1445.     begin
  1446.         if theWind = oldWindow then
  1447.             begin
  1448.                 oldWindow := nil;
  1449. {•    oldWDHandler := nil;•}
  1450.             end;
  1451.  
  1452.         if (whList <> nil) then
  1453.             begin
  1454.                 returnflag := false;
  1455.                 if whList^^.whWind = theWind then
  1456.                     begin
  1457.                         h2 := whlist;
  1458.                         whList := whList^^.whNext;
  1459.                     end
  1460.                 else
  1461.                     begin
  1462.                         h := whList;
  1463.                         while (h <> nil) and not returnflag do
  1464.                             begin
  1465.                                 h2 := h^^.whNext;
  1466.                                 if (h2 = nil) then
  1467.                                     begin
  1468.                                         h := nil;
  1469.                                         returnflag := true;
  1470.                                     end
  1471.                                 else if h2^^.whWind = theWind then
  1472.                                     begin
  1473.                                         h^^.whNext := h2^^.whNext;
  1474.                                         h := nil;
  1475.                                     end;
  1476.                                 if h <> nil then
  1477.                                     h := h2;
  1478.                             end;
  1479.                     end;
  1480.                 if not returnflag then
  1481.                     begin
  1482.                         if (whClobOnRmve) then
  1483.                             DoClobber(h2);
  1484.                         DisposHandle(Handle(h2));
  1485.                     end;
  1486.             end;
  1487.     end;
  1488.  
  1489. {$IFC supportDialogs }
  1490.  
  1491. { -------------------------------------------------------------------- }
  1492. {                    Dialog-handler interface routines                                }
  1493. { -------------------------------------------------------------------- }
  1494.  
  1495.  
  1496.  
  1497. {    Install a dialog handler.  Remove any previous handler for it.}
  1498. {    SkelDialog calls SkelWindow as a subsidiary to install a window}
  1499. {    handler, then sets the event procedure on return.}
  1500.  
  1501. {    Pass the following parameters:}
  1502.  
  1503. {    theDialog    Pointer to the dialog to be handled.  Must be created}
  1504. {            by host.}
  1505. {    pEvent    Event-handling proc for dialog events.}
  1506. {    pClose    Proc to execute when mouse clicked in close box.  Useful}
  1507. {            mainly to dialog handlers that want to know when to}
  1508. {            self-destruct (with SkelRmveDlog).}
  1509. {    pClobber Proc for disposal of handler's data structures}
  1510.  
  1511. {    If a particular procedure is not needed, pass nil in place of}
  1512. {    the appropriate procedure address.}
  1513.  
  1514. {    Return false if no handler could be allocated, true if successful.}
  1515.  
  1516.     function SkelDialog;
  1517.  
  1518.         var
  1519.             wh: WHandlerHnd;
  1520.             aBool: Boolean;
  1521.  
  1522.     begin
  1523.         aBool := SkelWindow(theDialog, nil, nil, nil, nil, pClose, pClobber, nil, false);
  1524.         if aBool <> false then
  1525.             begin
  1526.                 wh := GetWDHandler(theDialog);
  1527.                 wh^^.whEvent := pEvent;
  1528. {Added by Ingemar 18/9 -93:}
  1529.                 wh^^.whFilter := pFilter; {Install a filter function to be called *before* IsDialogEvent!}
  1530.             end;
  1531.         SkelDialog := aBool;
  1532.     end;
  1533.  
  1534. {    Remove a dialog and its handler}
  1535.  
  1536.     procedure SkelRmveDlog;
  1537.  
  1538.     begin
  1539.         SkelRmveWind(theDialog);
  1540.     end;
  1541. {$ENDC}
  1542. { -------------------------------------------------------------------- }
  1543. {                    Miscellaneous interface routines                    }
  1544. { -------------------------------------------------------------------- }
  1545.  
  1546.  
  1547. {    Override the default sizing limits for a window, or, if theWind}
  1548. {    is nil, reset the default limits used by SkelWindow.}
  1549.  
  1550.     procedure SkelGrowBounds;
  1551.  
  1552.         var
  1553.             h: WHandlerHnd;
  1554.             r: Rect;
  1555.  
  1556.     begin
  1557.         if theWind = nil then
  1558.             SetRect(growRect, hLo, vLo, hHi, vHi)
  1559.         else
  1560.             begin
  1561.                 h := GetWHandler(theWind);
  1562.                 if h <> nil then
  1563.                     begin
  1564.                         SetRect(r, hLo, vLo, hHi, vHi);
  1565.                         h^^.whGrow := r;
  1566.                     end;
  1567.             end;
  1568.     end;
  1569.  
  1570. {    Set the event mask.}
  1571.  
  1572.     procedure SkelEventMask;
  1573.  
  1574.     begin
  1575.         eventMask := mask;
  1576.     end;
  1577.  
  1578. {    Return the event mask.}
  1579.  
  1580.     procedure SkelGetEventMask;
  1581.  
  1582.     begin
  1583.         mask := eventMask;
  1584.     end;
  1585.  
  1586. {    Install a background task.  If p is nil, the current task is}
  1587. {    disabled.}
  1588.  
  1589.     procedure SkelBackground;
  1590.  
  1591.     begin
  1592.         pBkgnd := p;
  1593.     end;
  1594.  
  1595. {    Return the current background task.  Return nil if none.}
  1596.  
  1597.     procedure SkelGetBackground;
  1598.     begin
  1599.         p := pBkgnd;
  1600.     end;
  1601.  
  1602. {    Install an event-inspecting hook.  If p is nil, the hook is}
  1603. {    disabled.}
  1604.  
  1605.     procedure SkelEventHook;
  1606.  
  1607.     begin
  1608.         pEvent := p;
  1609.     end;
  1610.  
  1611.     procedure SkelGetEventHook;
  1612.  
  1613.     begin
  1614.         p := pEvent;
  1615.     end;
  1616. {$IFC supportDialogs }
  1617.  
  1618. {    Set the mask for event types that will be passed to dialogs.}
  1619. {    Bit 1 is always set, so that null events will be passed.}
  1620. {    If this is not done, the caret does not blink in editText items.}
  1621.  
  1622.     procedure SkelDlogMask;
  1623.  
  1624.     begin
  1625.         dlogEventMask := BitOr(mask, 1);
  1626.     end;
  1627.  
  1628. {    Return the current dialog event mask.}
  1629.  
  1630.     procedure SkelGetDlogMask;
  1631.  
  1632.     begin
  1633.         mask := dlogEventMask;
  1634.     end;
  1635. {$ENDC}
  1636. end.